package com.tianheng.wanzhou.datacollecting.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianheng.wanzhou.datacollecting.DataCollectingApplication;
import com.tianheng.wanzhou.datacollecting.db.entity.AirCompressorAmpsHis;
import com.tianheng.wanzhou.datacollecting.db.entity.AirCompressorPowerHis;
import com.tianheng.wanzhou.datacollecting.db.entity.MachineList;
import com.tianheng.wanzhou.datacollecting.db.service.IAirCompressorAmpsHisService;
import com.tianheng.wanzhou.datacollecting.db.service.IAirCompressorPowerHisService;
import com.tianheng.wanzhou.datacollecting.db.service.IMachineListService;
import com.tianheng.wanzhou.datacollecting.jedis.JedisPoolUtil;
import com.tianheng.wanzhou.datacollecting.util.SerializeUtil;
import com.tianheng.wanzhou.datacollecting.util.TqlHexchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @Author: yujw
 * @Date: 2020/6/22 14:26
 * @Version 1.0
 */
@Component
public class MachineSchedule {

    @Autowired
    IMachineListService machineListService;
    @Autowired
    JedisPoolUtil jedisPoolUtil;
//    @Autowired
//    IAirCompressorAmpsHisService ampsHisService;
//    @Autowired
//    IAirCompressorPowerHisService powerHisService;

//    private List<MachineList> machineList = new ArrayList<>();

//    @PostConstruct
//    private void postConstruct() {
//        QueryWrapper<MachineList> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("status", 1);
//        machineList = machineListService.list(queryWrapper);
//        Jedis jedis = jedisPoolUtil.getJedis();
//        jedis.flushDB();
//        for (MachineList machine : this.machineList) {
//            jedis.set(machine.getMacIp().getBytes(), SerializeUtil.serialize(machine));
//        }
//        jedis.close();
//    }
//
//    //隔一小时更新缓存
//    @Scheduled(cron = "0 0 0/1 * * ?")
////    @Scheduled(cron = "* * * * * ?")
//    public void updateMacList() {
//        QueryWrapper<MachineList> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("status", 1);
//        machineList = machineListService.list(queryWrapper);
//        Jedis jedis = jedisPoolUtil.getJedis();
//        jedis.flushDB();
//        for (MachineList machine : this.machineList) {
//            jedis.set(machine.getMacIp().getBytes(), SerializeUtil.serialize(machine));
//        }
//        jedis.close();
//    }
//
//    @Scheduled(cron = "0 0 0/1 * * ?")
//    public void saveAmpsHis() throws IOException, ClassNotFoundException {
//        Jedis jedis = jedisPoolUtil.getJedis();
//        Set<String> keys = DataCollectingApplication.ampsHis.keySet();
//        //Iterator迭代器
//        Iterator<String> iterator = keys.iterator();
//        while (iterator.hasNext()) {
//            MachineList machine = (MachineList) SerializeUtil.deserialize(jedis.get(iterator.next().getBytes()));
//            AirCompressorAmpsHis his = new AirCompressorAmpsHis();
//            his.setMotorAmps(DataCollectingApplication.ampsHis.get(iterator.next()).toString());
//            his.setMachineIp(machine.getMacIp());
//            his.setWorkshop(machine.getWorkshop());
//            his.setMachineId(machine.getMacId());
//            ampsHisService.save(his);
//        }
//        jedis.close();
//    }
//
//    @Scheduled(cron = "0 0/5 * * * ?")
//    public void savePowerHis() throws IOException, ClassNotFoundException {
//        Jedis jedis = jedisPoolUtil.getJedis();
//        Set<String> keys = DataCollectingApplication.powerHis.keySet();
//        Iterator<String> iterator = keys.iterator();
//        while (iterator.hasNext()) {
//            MachineList machine = (MachineList) SerializeUtil.deserialize(jedis.get(iterator.next().getBytes()));
//            AirCompressorPowerHis his = new AirCompressorPowerHis();
//            his.setPowerConsumption(DataCollectingApplication.powerHis.get(iterator.next()));
//            his.setMachineIp(machine.getMacIp());
//            his.setWorkshop(machine.getWorkshop());
//            his.setMachineId(machine.getMacId());
//            powerHisService.save(his);
//        }
//        jedis.close();
//    }
}
